#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QFont triggerButtonFont = QFont("Corbel Light", 12);
    triggerButton = new QPushButton(this);
    triggerButton->setFont(triggerButtonFont);
    triggerButton->setText("开始动画");
    triggerButton->setStyleSheet("margin-left:1px;");
    triggerButton->resize(100, 25);
    triggerButton->move(this->width()/2 - triggerButton->width()/2, this->height()/2 - triggerButton->height()/2);
    triggerButton->show();

    QFont animationLabelFont = QFont("Corbel ligth", 10);
    QFontMetrics animationLineFM(animationLabelFont);
    animationLine = new QLineEdit(this);
    animationLine->setFont(animationLabelFont);
    animationLine->setText("动画标签");
    animationLine->setReadOnly(true);
    animationLine->setMinimumHeight(animationLineFM.height());
    animationLine->setMaximumHeight(animationLineFM.size(Qt::TextSingleLine, "动画标签").width() + 10);
    animationLine->setStyleSheet("background-color:#00000000;border-style:none;border-width:0px;margin-left:1px;");
    animationLine->move(triggerButton->pos() +
                         QPoint(triggerButton->width()/2,
                                triggerButton->height() + animationLine->height()));
    std::cout << animationLine->width() << " " << animationLine->height() << std::endl;
    //设置按钮透明度
    QGraphicsOpacityEffect* labelOpacity = new QGraphicsOpacityEffect(triggerButton);
    labelOpacity->setOpacity(1);
    animationLine->setGraphicsEffect(labelOpacity);
    animationLine->show();

    //设置animationLine动画组（包含透明度和运动）
    QParallelAnimationGroup* dpGroup = new QParallelAnimationGroup;
    QSequentialAnimationGroup* widgetFadeIn = new QSequentialAnimationGroup;
    //运动动画
    QPropertyAnimation* animation = new QPropertyAnimation(animationLine, "pos", this);
    //淡入透明度动画
    QPropertyAnimation* fade = new QPropertyAnimation(labelOpacity, "opacity", animationLine);
    fade->setDuration(750);
    fade->setStartValue(0);
    fade->setEndValue(0.99);
    animation->setStartValue(animationLine->pos() - QPoint(300, 0));
    animation->setEndValue(animationLine->pos());
    animation->setDuration(600);
    animation->setEasingCurve(QEasingCurve::InOutExpo);
    dpGroup->addAnimation(fade);
    dpGroup->addAnimation(animation);
    //尝试triggerButton旋转动画
    QPropertyAnimation* rotate = new QPropertyAnimation(triggerButton, "rotationAngle", this);
    rotate->setDuration(750);
    rotate->setStartValue(0);
    rotate->setEndValue(90);
    rotate->setEasingCurve(QEasingCurve::InOutExpo);
    //开始动画
    connect(triggerButton, &QPushButton::clicked, this, [=]() {
        dpGroup->start();
        rotate->start();
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

